// pages/User/AddIndividualBusiness/AddIndividualBusiness.js

//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { saveSelfEmplooyed , querySelfEmployedPolitical , querySelfEmployedEdulevel , querySelfEmployedCareer } from '../../../api/employed';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    uploadFileUrl: app.globalData.global.uploadFileUrl,
    checked: false,
    idCardFront: '', // 身份证正面照
    idCardBack: '', // 身份证反面照
    oneInchPhoto: '', // 一寸照
    bankName: '', // 银行名称
    bankCard: '', // 银行卡号
    email: '', // 电子邮箱
    mobile: '', // 手机号
    idCard: '', // 身份证号
    realName: '', // 姓名
    laborCompanyId: '', // 劳务公司ID
    industry: '', // 行业名称
    residence: '', // 住所
    postCode: '', // 邮政编码
    politicalStatus: '', // 政治面貌
    nation: '', // 民族
    eduLevel: '', // 文化程度
    careerStatus: '', // 职业状态
    signPicUrl: '', // 客户签名图片
    politicalList: [], // 政治面貌下拉列表
    edulevelList: [], // 文化程度下拉列表
    careerList: [], // 职业状况下拉列表
    columns: [], // 选择框信息
    columnsType: '', // 选择框类型
    showPicker: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.laborCompanyId){
        console.log(JSON.parse(options.industry),'item')
        this.setData({
            laborCompanyId: JSON.parse(options.laborCompanyId),
            industry: JSON.parse(options.industry),
        })
    }
    this._querySelfEmployedPolitical();
    this._querySelfEmployedEdulevel();
    this._querySelfEmployedCareer();
    var _this = this;
    if (wx.onNeedPrivacyAuthorization){
      wx.onNeedPrivacyAuthorization((resolve) => {
        console.log('触发弹窗')
        // 触发弹窗
        _this.setData({
          showPrivacyAuthor: true,
        })
        _this.resolvePrivacyAuthorization = resolve
        console.log(resolve,resolve);
      })
    }  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onCheckChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
    // 用户协议
    showAgreement(){
        var url = `${app.globalData.global.assetsIp}/appLink/#/yonghufuwuyinsixieyi`;
        wx.navigateTo({
          url: `/pages/link/link/link?url=${encodeURIComponent(url)}&title=${JSON.stringify('用户协议')}`,
        })
    },
    // 确认申请
    apply(){
        let _this = this;
        console.log(_this.data,'data');
        const { checked , idCardFront , idCardBack , oneInchPhoto , bankName , bankCard , email , mobile , idCard , realName , residence , postCode , politicalStatus , nation , eduLevel , careerStatus , signPicUrl } = _this.data;
        if(!checked){
            wx.showToast({
              title: '请阅读并同意用户协议！',
              icon: 'none',
            })
            return;
        }
        if(idCardFront == ''){
            wx.showToast({
                title: '请上传经营者身份证正面照！',
                icon: 'none',
              })
            return;
        }
        if(idCardBack == ''){
            wx.showToast({
                title: '请上传经营者身份证反面照！',
                icon: 'none',
              })
            return;
        }
        if(oneInchPhoto == ''){
            wx.showToast({
                title: '请上传经营者一寸证照！',
                icon: 'none',
              })
            return;
        }
        if(realName == ''){
            wx.showToast({
                title: '请输入姓名',
                icon: 'none',
              })
            return;
        }
        if(idCard == ''){
            wx.showToast({
                title: '请输入身份证号码',
                icon: 'none',
              })
            return;
        }
        if(mobile == ''){
            wx.showToast({
                title: '请输入法人联系手机号',
                icon: 'none',
              })
            return;
        }
        if(email == ''){
            wx.showToast({
                title: '请输入电子邮箱',
                icon: 'none',
              })
            return;
        }
        if(bankCard == ''){
            wx.showToast({
                title: '请输入银行卡号',
                icon: 'none',
              })
            return;
        }
        if(bankName == ''){
            wx.showToast({
                title: '请输入银行名称',
                icon: 'none',
              })
            return;
        }
        saveSelfEmplooyed(_this.data)
        .then(res => {
            if(res.success){
                wx.setStorageSync('hideSelCompany', '1');
                wx.showToast({
                    title: '提交成功',
                    icon: 'none',
                })
                wx.navigateBack({
                    delta: 2
                })
            }else{
                wx.showToast({
                    title: res.errMessage,
                    icon: 'none',
                })
            }
        })
    },
    // 上传身份证这你按照
    afterRead(event){
        let _this = this;
        const { file } = event.detail;
        const name = event.target.dataset.name||event.currentTarget.dataset.name;
        // console.log(event,'event')
        wx.uploadFile({
            url: _this.data.uploadFileUrl, // 上传接口
            filePath: file.url,
            name: 'multipartFile',
            header: {
                'auth-token':wx.getStorageSync('token')
            },
            success(res) {
                console.log(res,'res')
                var res = JSON.parse(res.data);
                if(res.success){
                    // var types = checkFile(res.data);
                    // if(types == 'video'){
                    // fileList.push({url: res.data,isVideo: true,deletable: true})
                    // }else if(types == 'image'){
                    // fileList.push({url: res.data,isImage: true,deletable: true})
                    // }
                    // console.log(fileList,'fileList')
                    // _this.setData({ fileList });
                    switch (name) {
                        case 'fornt':
                            _this.setData({
                                idCardFront: res.data,
                            })
                            break;
                        case 'back':
                            _this.setData({
                                idCardBack: res.data,
                            })
                            break;
                        case 'oneInch':
                            _this.setData({
                                oneInchPhoto: res.data,
                            })
                            break;
                        default:
                            break;
                    }
                }else{
                    wx.showToast({
                        title: res.errMessage,
                        icon: 'none',
                    })
                }
            },
        });
    },
    // 输入框
    onChange(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
        const name = event.target.dataset.name||event.currentTarget.dataset.name;
        console.log(name,'name')
        // this.setData({
        //     name: event.detail,
        // })
        var data = this.data;
        data[name] =  event.detail;
        this.setData(data);
    },
    // 获取政治面貌列表数据
    _querySelfEmployedPolitical(){
      let _this = this;
      querySelfEmployedPolitical()
      .then(res => {
        if(res.success){
          _this.setData({
            politicalList: res.data,
          })
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none'
          })
        }
      })
    },
    // 获取文化程度下拉列表
    _querySelfEmployedEdulevel(){
      let _this = this;
      querySelfEmployedEdulevel()
      .then(res => {
        if(res.success){
          _this.setData({
            edulevelList: res.data,
          })
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    },
    // 获取职业状况
    _querySelfEmployedCareer(){
      let _this = this;
      querySelfEmployedCareer()
      .then(res => {
        if(res.success){
          _this.setData({
            careerList: res.data,
          })
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    },
    // 打开政治面貌、文化程度、工作状态选择框
    showPick(e){
      let _this = this;
      let type = e.target.dataset.type||e.currentTarget.dataset.type;
      let list = [];
      switch (type) {
        case 'politicalStatus':
          list = _this.data.politicalList;
          break;
        case 'eduLevel':
          list = _this.data.edulevelList;
          break;
        case 'careerStatus':
          list = _this.data.careerList;
          break;
        default:
          break;
      }
      _this.setData({
        columns: list,
        columnsType: type,
        showPicker: true,
      })
    },
    onClose(){
      this.setData({
        showPicker: false
      })
    },
    // 确认选择政治面貌、文化程度、工作状态
    onPickerConfirm(e){
      console.log(e.detail.value,'e')
      let name = this.data.columnsType;
      var data = this.data;
      data[name] =  e.detail.value;
      data.showPicker = false;
      this.setData(data);
    },
    // 去签名
    goSign(){
      let _this = this;
      wx.navigateTo({
        url: '/pages/User/Signature/Signature',
        events: {
          'updateSignature': function(res){
            console.log(res,'res')
            if(res){
              _this.setData({
                signPicUrl: res
              })
            }
          }
        }
      })
    },
})